<style lang="less">
@import "./login.less";
</style>

<template>
  <div class="login">
    <div class="login-con">
      <Card icon="log-in" title="欢迎登录" :bordered="false">
        <div class="form-con">
          <Form ref="loginForm" :model="form" :rules="rules">
            <FormItem prop="username">
              <Input v-model="form.username" placeholder="请输入用户名">
                <span slot="prepend">
                  <Icon :size="16" type="ios-person"></Icon>
                </span>
              </Input>
            </FormItem>
            <FormItem prop="password">
              <Input type="password" v-model="form.password" placeholder="请输入密码">
                <span slot="prepend">
                  <Icon :size="14" type="md-lock"></Icon>
                </span>
              </Input>
            </FormItem>
            <FormItem>
              <Button @click="submit" type="primary" long>登录</Button>
            </FormItem>
          </Form>
        </div>
      </Card>
    </div>
  </div>
</template>

<script>
import { setToken, getToken, setUser, getUser } from "@/libs/util.js";
import { mapActions } from "vuex";
import * as userApi from "@/api/user.js";
export default {
  props: {
    usernameRules: {
      type: Array,
      default: () => {
        return [{ required: true, message: "账号不能为空", trigger: "blur" }];
      }
    },
    passwordRules: {
      type: Array,
      default: () => {
        return [{ required: true, message: "密码不能为空", trigger: "blur" }];
      }
    }
  },
  data() {
    return {
      form: {
        username: "",
        password: ""
      }
    };
  },
  computed: {
    rules() {
      return {
        username: this.usernameRules,
        password: this.passwordRules
      };
    }
  },
  methods: {
    ...mapActions(["getUserInfo"]),
    submit: function() {
      const msg = this.$Message.loading({
        content: "正在登陆...",
        duration: 0
      });
      setTimeout(msg, 1000);
      userApi.userSubmit(this.form).then(res => {
        if (res.data.code == 200) {
          if (res.data.data.email == null) {
            setToken("admin");
            setUser("user" + res.data.data.userId);
          } else {
            setToken("shop");
            setUser("shop" + res.data.data.shopId);
          }
          this.getUserInfo().then(res => {
            this.$router.push({
              name: this.$config.homeName
            });
          });
        } else {
          this.$Message.error(res.data.message);
        }
      });
    }
  }
};
</script>

<style>
</style>
